/******************************************************************************
 * * contact_global.h - data structure for contact module
 *
 * *(C) Copyright 2019 Asr International Ltd.
 * * All Rights Reserved
 * ******************************************************************************/
#ifndef CONTACT_GLOBAL_H
#define CONTACT_GLOBAL_H

#ifdef __cplusplus
extern "C" {
#endif

/* include header file */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "ui_type.h"
#include "ui_log.h"
#include "ui_textid.h"
#include "modem/mmi_modemadp_interface.h"
#include "ui_nvm_interface.h"
#include "inter_common_ui_interface.h"
#include "inter_contacts_interface.h"
#include "inter_framework_interface.h"
#include "inter_setting_interface.h"
#include "nav.h"
#include "../../lvgl/hal/hal.h"
#include "ui_ime.h"

/* define micro */
#define CONTACTS_TIMER_STATUS_INTERFACE_LENGTH    2000

#define CONTACTS_MAX_NAME_LENGTH                  NV_CONTACTS_MAX_NAME_LEN
#define CONTACTS_MAX_NUMBER_LENGTH                NV_CONTACTS_MAX_NUMBER_LEN

#define CONTACTS_SIM_NAME_MAX_CHAR_NUM_WITHOUT_CHINESE 14
#define CONTACTS_SIM_NAME_MAX_CHAR_NUM_WITH_CHINESE    6

#define CONTACTS_UTF8_BYTE0_E4                    0xE4
#define CONTACTS_UTF8_BYTE0_E5                    0xE5
#define CONTACTS_UTF8_BYTE0_E6                    0xE6
#define CONTACTS_UTF8_BYTE0_E7                    0xE7
#define CONTACTS_UTF8_BYTE0_E8                    0xE8
#define CONTACTS_UTF8_BYTE0_E9                    0xE9

#define CONTACTS_UTF8_BYTE1_4BIT_80               0x80
#define CONTACTS_UTF8_BYTE1_4BIT_90               0x90
#define CONTACTS_UTF8_BYTE1_4BIT_A0               0xA0
#define CONTACTS_UTF8_BYTE1_4BIT_B0               0xB0

#define CONTACTS_MAX_TRANSLATE_GB_CODE            0xFEFF

#define CONTACTS_UTF8_TO_GB_CODE_MAX_NUM          6763

#define CONTACTS_E4_B_MAX_NUM                     8

#define CONTACTS_E5_8_MAX_NUM                     16
#define CONTACTS_E5_9_MAX_NUM                     16
#define CONTACTS_E5_A_MAX_NUM                     16
#define CONTACTS_E5_B_MAX_NUM                     16

#define CONTACTS_E6_8_MAX_NUM                     16
#define CONTACTS_E6_9_MAX_NUM                     16
#define CONTACTS_E6_A_MAX_NUM                     15
#define CONTACTS_E6_B_MAX_NUM                     16

#define CONTACTS_E7_8_MAX_NUM                     16
#define CONTACTS_E7_9_MAX_NUM                     16
#define CONTACTS_E7_A_MAX_NUM                     16
#define CONTACTS_E7_B_MAX_NUM                     15

#define CONTACTS_E8_8_MAX_NUM                     16
#define CONTACTS_E8_9_MAX_NUM                     16
#define CONTACTS_E8_A_MAX_NUM                     15
#define CONTACTS_E8_B_MAX_NUM                     14

#define CONTACTS_E9_8_MAX_NUM                     15
#define CONTACTS_E9_9_MAX_NUM                     15
#define CONTACTS_E9_A_MAX_NUM                     11
#define CONTACTS_E9_B_MAX_NUM                     10

#define CONTACTS_SB_MIN_SIZE                      (LV_DPI / 8)
#define CONTACTS_MATH_MAX(a,b)                    ((a) > (b) ? (a) : (b))

/* structure for global variate */
typedef VOID (*ContactCallback)(VOID*);
typedef struct {
    UINT16 GBCode;
    UINT8  UTF8CodeByte3;
} Contacts_UTF8_To_GB2312_Code_t;

typedef struct {
    UINT8  UTF8Byte2;
    UINT8  GBCnt;
    UINT32 StartIndex;
} Contacts_UTF8_To_GB2312_Code_Index_t;

enum {
    CONTACTS_MEMORY_PHONE = 0,
    CONTACTS_MEMORY_SIM1,
    CONTACTS_MEMORY_SIM2,
    CONTACTS_MEMORY_MAX_NUM
};
typedef UINT8 CONTACTS_MEMORY;

typedef struct {
    UINT8 ToneIndex;
    BOOL  MarkFlg;                          // TRUE: mark, FALSE: unmark
    UINT16 MemoryAndIndex;                  /* bit 14 and 15: CONTACTS_MEMORY
                                             * bit 0 – 13: index in SIM1/SIM2/PHONE */
    INT8 Name[CONTACTS_MAX_NAME_LENGTH];
    INT8 Number[CONTACTS_MAX_NUMBER_LENGTH];
} Contacts_Info_t;

typedef struct {
    Contacts_Info_t *ContactInfo;
} Contacts_List_t;

typedef struct {
    UINT16 Total; // default 0
    UINT16 Used;
    UINT8  FirstIndex;
} Contacts_Phonebook_Status_t;

typedef struct {
    INT8 Name[CONTACTS_MAX_NAME_LENGTH];
    INT8 Number[CONTACTS_MAX_NUMBER_LENGTH];
} Contacts_Speed_Dial_Info_t;

enum {
    CONTACTS_ACTION_NULL,
    CONTACTS_ACTION_COPY,
    CONTACTS_ACTION_COPYING,
    CONTACTS_ACTION_MOVE,
    CONTACTS_ACTION_MOVING,
    CONTACTS_ACTION_DELETE,
    CONTACTS_ACTION_ADD,
    CONTACTS_ACTION_ADD_REQ,
    CONTACTS_ACTION_REPLACE_PHASE_1, // delete the original contact
    CONTACTS_ACTION_REPLACE_PHASE_2, // add the new contact
    CONTACTS_ACTION_EDIT_PHASE_1,    // delete the original contact
    CONTACTS_ACTION_EDIT_PHASE_2     // add the new contact
};
typedef UINT8 CONTACTS_ACTION;

typedef struct {
    CONTACTS_ACTION Action;
    CONTACTS_MEMORY SrcMemory;
    CONTACTS_MEMORY DestMemory;
    UINT16          MarkNum;    // marked contact number
    lv_ll_t         SrcMemList; // only for Contacts copy/move
    BOOL            NameModifedFlg;
    BOOL            ProcQuit;
    UINT16          NumberOfNoNumContact;
    UINT8           WaitCnfBitmap;
} Contacts_Operation_t;

enum {
    CONTACTS_PHONEBOOK_STATUS_UNKNOWN = 0,
    CONTACTS_PHONEBOOK_STATUS_UNKNOWN_INIT, // SIM present, wait for sim status ind from framework
    CONTACTS_PHONEBOOK_STATUS_SIM_ABSENT,   // SIM absent
    CONTACTS_PHONEBOOK_STATUS_NOT_INIT,     // SIM present, wait for phonebook init ind
    CONTACTS_PHONEBOOK_STATUS_INIT,         // Phonebook init ind received, prepare to read phonebook
    CONTACTS_PHONEBOOK_STATUS_READING,      // Get phonebook status, and read phonebook
    CONTACTS_PHONEBOOK_STATUS_READ_DONE     // Phonebook has been read
};
typedef UINT8 CONTACTS_PHONEBOOK_STATUS;

typedef struct {
    UINT16          InterfaceId;
    VOID            *Ctrl;         // controls pointer of interface except Contacts interface
    Contacts_List_t *FocusContact; // only for Contacts interface
    VOID            *PrevCtrl;     // for previous interface to change description
    Contacts_List_t *Focus;        // for other interface except Contacts interface
    INT8            *Number;
    ACTIVITY_ID     SrcActId;
    BOOL            SelAllFlg;     // for contacts copy/move/delete interface
    Contacts_Info_t *ContactDetail;
} Contacts_If_Ctrl_t;

typedef struct {
#define CONTACTS_MEMSEL_PHONE  (0x01 << CONTACTS_MEMORY_PHONE)
#define CONTACTS_MEMSEL_SIM1   (0x01 << CONTACTS_MEMORY_SIM1)
#define CONTACTS_MEMSEL_SIM2   (0x01 << CONTACTS_MEMORY_SIM2)
#define CONTACTS_MEMSEL_ALL    (CONTACTS_MEMSEL_PHONE | CONTACTS_MEMSEL_SIM1 | CONTACTS_MEMSEL_SIM2)
    UINT8   MemSel;         // bitmap
    lv_ll_t ListForSpecMem; // Contacts_List_t *
    UINT16  ContactsCount;
} Contacts_Mem_Sel_Setting_t;

typedef struct {
    lv_ll_t         SearchList; // Contacts_List_t *
    UINT16          ContactsCount;
    Contacts_List_t *FocusContact;
} Contacts_Search_t;

typedef struct {
    Contacts_If_Ctrl_t          InterfaceCtrl;
    Contacts_Phonebook_Status_t PhkStatus[CONTACTS_MEMORY_MAX_NUM];                        // phone & SIM1 & SIM2
    lv_ll_t                     ContactsList;                                              // Contacts_List_t
    UINT16                      ContactsCount;
    Contacts_Mem_Sel_Setting_t  ContactsMemSel;
#define CONTACTS_MAX_NUM_SCRN_NUMBER          NV_CONTACTS_MAX_NUMBER_IN_SCRN_LIST_NUM                  // for number screening list
    INT8                        NumScrnList[CONTACTS_MAX_NUM_SCRN_NUMBER][CONTACTS_MAX_NUMBER_LENGTH
    ];                                                                                                 // phone number list
#define CONTACTS_MAX_SPEED_DIAL_KEY_NUMBER    NV_CONTACTS_MAX_SPEED_DIAL_KEY_NUM
    Contacts_Speed_Dial_Info_t  SpeedDialList[CONTACTS_MAX_SPEED_DIAL_KEY_NUMBER];                     // Name and number for key 2 – 9
    CONTACTS_PHONEBOOK_STATUS   PhonebookStatus[2];                                                    // for reading phonebook after power on
    Contacts_Operation_t        ContactOper;                                                           // copy,move,delete
    TIMER_ID                    TimerId;
    BOOL                        TimerRunFlg;
    UINT8                       SelectContactType;                                                     // IF_CONTACTS_SEL_CONTACT_TYPE
    Contacts_Search_t           ContactsSearch;
} Contacts_Mng_t;

/* extern global variate */
extern Contacts_Mng_t g_ContactsMng;
extern const Contacts_UTF8_To_GB2312_Code_Index_t E4_BStart[CONTACTS_E4_B_MAX_NUM];
extern const Contacts_UTF8_To_GB2312_Code_Index_t E5_8Start[CONTACTS_E5_8_MAX_NUM];
extern const Contacts_UTF8_To_GB2312_Code_Index_t E5_9Start[CONTACTS_E5_9_MAX_NUM];
extern const Contacts_UTF8_To_GB2312_Code_Index_t E5_AStart[CONTACTS_E5_A_MAX_NUM];
extern const Contacts_UTF8_To_GB2312_Code_Index_t E5_BStart[CONTACTS_E5_B_MAX_NUM];
extern const Contacts_UTF8_To_GB2312_Code_Index_t E6_8Start[CONTACTS_E6_8_MAX_NUM];
extern const Contacts_UTF8_To_GB2312_Code_Index_t E6_9Start[CONTACTS_E6_9_MAX_NUM];
extern const Contacts_UTF8_To_GB2312_Code_Index_t E6_AStart[CONTACTS_E6_A_MAX_NUM];
extern const Contacts_UTF8_To_GB2312_Code_Index_t E6_BStart[CONTACTS_E6_B_MAX_NUM];
extern const Contacts_UTF8_To_GB2312_Code_Index_t E7_8Start[CONTACTS_E7_8_MAX_NUM];
extern const Contacts_UTF8_To_GB2312_Code_Index_t E7_9Start[CONTACTS_E7_9_MAX_NUM];
extern const Contacts_UTF8_To_GB2312_Code_Index_t E7_AStart[CONTACTS_E7_A_MAX_NUM];
extern const Contacts_UTF8_To_GB2312_Code_Index_t E7_BStart[CONTACTS_E7_B_MAX_NUM];
extern const Contacts_UTF8_To_GB2312_Code_Index_t E8_8Start[CONTACTS_E8_8_MAX_NUM];
extern const Contacts_UTF8_To_GB2312_Code_Index_t E8_9Start[CONTACTS_E8_9_MAX_NUM];
extern const Contacts_UTF8_To_GB2312_Code_Index_t E8_AStart[CONTACTS_E8_A_MAX_NUM];
extern const Contacts_UTF8_To_GB2312_Code_Index_t E8_BStart[CONTACTS_E8_B_MAX_NUM];
extern const Contacts_UTF8_To_GB2312_Code_Index_t E9_8Start[CONTACTS_E9_8_MAX_NUM];
extern const Contacts_UTF8_To_GB2312_Code_Index_t E9_9Start[CONTACTS_E9_9_MAX_NUM];
extern const Contacts_UTF8_To_GB2312_Code_Index_t E9_AStart[CONTACTS_E9_A_MAX_NUM];
extern const Contacts_UTF8_To_GB2312_Code_Index_t E9_BStart[CONTACTS_E9_B_MAX_NUM];
extern const Contacts_UTF8_To_GB2312_Code_t UTF8_To_GB_Code[CONTACTS_UTF8_TO_GB_CODE_MAX_NUM];

/* extern function */
extern UINT8 Contacts_Get_Memory_Select(CONTACTS_MEMORY Memory);
extern UINT16 Contacts_Set_Contact_Memory(CONTACTS_MEMORY Memory);
extern CONTACTS_MEMORY Contacts_Get_Contact_Memory(UINT16 MemoryAndIndex);
extern UINT16 Contacts_Get_Contact_Index(UINT16 MemoryAndIndex);
extern BOOL Contacts_Is_In_MemSel(UINT16 MemoryAndIndex, UINT8 MemSel);
extern VOID Contacts_Timer_Stop(VOID);
extern VOID Contacts_Status_Interface_Timer_Start(ContactCallback Cb);
extern VOID Contacts_Timeout_Back_To_Prev_Interface(VOID* Para);
extern VOID Contacts_Press_Direction_Key_On_List(UI_KEY_VALUE Key_Val);
extern VOID Contacts_Interface_OnSaveState(VOID *Ctrl);
extern VOID Contacts_Normal_List_Interface_OnDestroy(VOID *Ctrl);
extern VOID Contacts_Update_ContBtn_Des(Contacts_Info_t *Contact, UI_Button_Desc_t *ContBtn);
extern INT8 *Contacts_Get_Contact_Memory_Symbol(UINT16 MemoryAndIndex);
extern VOID Contacts_Status_Interface_OnCreate(VOID *Ctrl);
extern VOID Contacts_Create_Contacts_Details(Contacts_Info_t *Contact, BOOL BackToContacts);
extern UINT8 Contacts_Status_Interface_Key_Cb(lv_obj_t *Obj, UI_KEY_STATUS Key_Sta, UI_KEY_VALUE Key_Val);
extern VOID Contacts_Interface_OnDestroy(VOID *Ctrl);
extern UINT8 Contacts_Press_Direction_Key(UI_KEY_VALUE Key_Val, UI_Button_Container_Desc_t *Interface, Contacts_List_t **Focus);
extern VOID Contacts_Create_Contacts_Noscreen_Add_Number(INT8 *Text);
extern VOID Contacts_Com_Create_Select_One_Contact(ACTIVITY_ID ForwardId, lv_event_cb_t BtnRelFun);
extern UINT8 Contacts_Com_Select_Multi_Press_Direction_Key(UI_KEY_VALUE Key_Val);
extern VOID Contacts_Com_Create_Select_Multi_Contacts(ACTIVITY_ID DestActId, UI_Handle_Key_CallBack *KeyFunc);
extern UINT8 Contacts_Normal_List_Interface_Key_Cb(lv_obj_t *Obj, UI_KEY_STATUS Key_Sta, UI_KEY_VALUE Key_Val);
extern lv_ll_t *Contacts_Get_Contacts_Operation_List(UINT16 *Count);
extern UINT8 Contacts_Add_Contact(Contacts_Info_t *New, Contacts_List_t **Focus, Contacts_List_t **FocusForContacts);
extern void Contacts_Btn_Contact_Cb(lv_obj_t *Btn,lv_event_t event);
extern VOID Contacts_Create_Contacts_Saved(UINT16 TxtId);
extern VOID Contacts_Create_Contacts_Noscreen_Delete(UINT8 Index);
extern VOID Contacts_Add_Contact_To_Nvm(Contacts_Info_t *ContactInfo);
extern VOID Contacts_Add_Contact_With_Name_Modified(Contacts_Info_t *ContactInfo, Contacts_List_t **FocusForContacts);
extern VOID Contacts_Speed_Dials_Update_Contact(Contacts_Info_t *Contact, INT8 *Name, INT8 *Number);
extern VOID Contacts_Set_ScrlBar_Pos_For_BtnCont(lv_obj_t * scrlbar, lv_obj_t * page, lv_coord_t scrl_h, lv_coord_t scrl_y);
extern VOID Contacts_Set_ScrlBar_Size_For_BtnCont(lv_obj_t * scrlbar, lv_obj_t * page, lv_coord_t scrl_h);
extern VOID Contacts_Input_Handler(UINT8 Len);
extern VOID Contacts_Process_Copy_End_Handle(VOID);
extern VOID Contacts_Process_Move_End_Handle(VOID);
extern VOID Contacts_Process_Delete_End_Handle(VOID);
extern VOID Contacts_Send_Write_Phonebook_Req(CONTACTS_MEMORY Memory, char *Name, char *Number, UINT16 Index);
extern VOID Contacts_Adjust_Name_Len_For_Sim(INT8 *Text);
extern INT32 Contacts_Compare_Number(INT8 *New, INT8 *Old);

extern VOID Contacts_Create_Contacts_Empty(VOID);
extern VOID Contacts_Create_Contacts_Add_Sel_Mem(VOID);
extern VOID Contacts_Create_Contacts_Search(VOID);
extern VOID Contacts_Create_Contacts_Copy_Select(VOID);
extern VOID Contacts_Create_Contacts_Move_Select(VOID);
extern VOID Contacts_Create_Contacts_Delete(VOID);
extern VOID Contacts_Create_Contacts_Settings(VOID);
extern VOID Contacts_Create_Contacts_Speed_Dials(VOID);
extern VOID Contacts_Create_Contacts_Noscreen(VOID);
extern VOID Contacts_Create_Contacts_Deleted(VOID);
extern VOID Contacts_Process_Delete_Multiple_Contacts(VOID);
extern VOID Contacts_Reset_Contacts_Interface(VOID);
extern VOID Contacts_Create_Contacts_Full(VOID);
extern VOID Contacts_Create_Contacts_Empty(VOID);
extern VOID Contacts_Create_Contacts_Copied(VOID);
extern VOID Contacts_Create_Contacts_Copy_Quit(VOID);
extern VOID Contacts_Process_Copy_Multiple_Contacts(VOID);
extern VOID Contacts_Create_Contacts_Moved(VOID);
extern VOID Contacts_Create_Contacts_Move_Quit(VOID);
extern VOID Contacts_Process_Move_Multiple_Contacts(VOID);
extern VOID Contacts_Update_Contacts_Interface(VOID);
extern VOID Contacts_Create_Contacts_Num_Scrn_List_Full(VOID);
extern VOID Contacts_Create_Contacts_Add_Num_Scrn(VOID);
extern VOID Contacts_Make_Src_Mem_List(VOID);
extern VOID Contacts_Create_No_Phone_Number(VOID);

#ifdef __cplusplus
} /* extern "C" */
#endif

#endif /* CONTACT_GLOBAL_H */
